package tech.cn.note.word.fun;

import cn.hutool.core.date.DatePattern;
import com.deepoove.poi.render.compute.RenderDataCompute;
import tech.cn.note.utils.DateTimeUtils;

import org.springframework.stereotype.Service;

import static org.springframework.util.ObjectUtils.isEmpty;

@Service
public class DateFormat implements RenderFunction {
    @Override
    public String doCalculate(String[] fields, RenderDataCompute renderDataCompute) {
        if (isEmpty(fields)) {
            return "";
        }

        // 默认 yyyy-MM-dd
        String dateFormatPattern = DatePattern.NORM_DATE_PATTERN;
        if (fields.length > 1) {
            dateFormatPattern = fields[1];
        }
        String datePlaceHolder = fields[0];
        Object date = renderDataCompute.compute(datePlaceHolder);
        if (date == null) {
            return "";
        }
        return DateTimeUtils.localDateParseToStr(DateTimeUtils.toLocalDate(date), dateFormatPattern);
    }
}
